package sdu.icat.pj.base;

import java.util.HashMap;

/**
 * @author ljh_2015
 *
 */
@SuppressWarnings("serial")
public class ResponseMap extends HashMap<String,Object>{

    private final int SUCCESS = 200;
    
    private final int NOLOGIN = 100;
    
    private final int ERROR = -1;
    
    public static ResponseMap newInstance() {
	return new ResponseMap();
    }
    
    public void putMsg(String msg) {
	this.put("msg", msg);
    }
    
    public void putMsg(String prefix,String msg) {
	this.put(prefix+"msg", msg);
    }
    
    public void setStatus(int status) {
	this.put("status", status);
    }
    
    public void setStatus(String prefix,int status) {
	this.put(prefix+"status", status);
    }
    
    public ResponseMap setAttribute(String attr,Object value) {
	this.put(attr, value);
	return this;
    }
    
    public void setData(Object obj) {
	this.put("data", obj);
    }
    
    public ResponseMap setSuccessMap(String msg,Object obj) {
	return setResultMap(SUCCESS, msg, obj);
    }
    
    public ResponseMap setFailedMap(String msg,Object obj) {
	return setResultMap(ERROR, msg, obj);
    }
    
    public ResponseMap setFailedNoLoginMap(String msg,Object obj) {
	return setResultMap(NOLOGIN, msg, obj);
    }
    
    public ResponseMap setResultMap(int status,String msg,Object obj) {
 	if(status!=0) {
 	    setStatus(status);
 	}
 	if(msg.length()>0) {
 	    putMsg(msg);
 	}
 	if(obj!=null) {
 	    setData(obj);
 	}
 	return this;
     }
}
